from flask import Flask, Blueprint,request, jsonify,send_from_directory
import requests





# 创建sys
web_bp = Blueprint('h5', __name__, url_prefix='/h5')


@web_bp.route('/static/temp/<path:pdf_path>', methods=['GET', 'POST'])
def pdf(pdf_path):
    return send_from_directory('static', f'temp/{pdf_path}')


# 用户管理
@web_bp.route('/<path:category_path>', methods=['GET','POST'])
def web(category_path):
    # 获取请求的所有信息
    url = f'http://localhost:5001/{category_path}'  # 新路由的地址
    headers = {key: value for key, value in request.headers if key != 'Host'}  # 复制请求头
    data = request.get_json() if request.is_json else request.form  # 获取请求体

    # 转发请求到新路由
    response = requests.request(
        method=request.method,
        url=url,
        headers=headers,
        json=data if request.is_json else None,
        data=data if not request.is_json else None,
        params=request.args,
        cookies=request.cookies  # 转发 cookies（包括 session 信息）
    )

    # 返回新路由的响应
    return jsonify(response.json()), response.status_code
# 用户管理

def register_web_bps(app: Flask):
    # 在admin_bp下注册子蓝图
    app.register_blueprint(web_bp)